﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TV.Translation.Library.Base;
using System.Xml.Serialization;
using System.Xml;

namespace TV.Translation.Library.Harris
{
    [Serializable]
    public class HDateTimeFrames : DateTimeFrames
    {
        public override void setValue(String input)
        {
            try
            {
                if (String.IsNullOrEmpty(input))
                {
                    return;
                }
                String strToParse = input.Substring(0, input.IndexOf('.'));
                int YYYY = Convert.ToInt32(strToParse.Substring(0, 4));
                int MM = Convert.ToInt32(strToParse.Substring(4, 2));
                int DD = Convert.ToInt32(strToParse.Substring(6, 2));
                int HH = Convert.ToInt32(strToParse.Substring(9, 2));
                int mm = Convert.ToInt32(strToParse.Substring(12, 2));
                int ss = Convert.ToInt32(strToParse.Substring(15, 2));
                m_dateTime = new DateTime(YYYY, MM, DD, HH, mm, ss);
                String strFrames = input.Substring(input.IndexOf('.') + 1);
                m_frames = Convert.ToInt32(strFrames);
            }
            catch (Exception ex)
            {
                String err = ex.ToString();
            }
        }

        public override string ToString()
        {
            return m_dateTime.ToString("YYYYMMDD hh:mm:ss");
        }
    }

    [Serializable]
    public class HTimeSpanFrames : TimeSpanFrames
    {
        public override void setValue(String input)
        {
            if (String.IsNullOrEmpty(input))
            {
                return;
            }
            String strToParse;
            String strFrames;
            if (input.IndexOf('.') < 0)
            {
                //no frames
                TimeSpan.TryParse(input, out m_timeSpan);
                m_frames = 0;
            }
            else
            {
                strToParse = input.Substring(0, input.IndexOf('.'));
                TimeSpan.TryParse(strToParse, out m_timeSpan);
                strFrames = input.Substring(input.IndexOf('.') + 1);
                m_frames = Convert.ToInt32(strFrames);
            }
        }

        public override string ToString()
        {
            return m_timeSpan.ToString();
        }
    }

    [Serializable]
    public class HSeriesEvent : BroadcastEvent
    {

        public HSeriesEvent()
        {
            startTime = new HDateTimeFrames();
            duration = new HTimeSpanFrames();
            sevtype = -1;
            spottype = -1;
        }

        #region Fields

        protected Int32 sevtype;
        protected Int32 spottype;

        #endregion Fields


        #region Properties

        [XmlAttribute(AttributeName = "eventid")]
        public String EventID
        {
            get { return id; }
            set { id = value; }
        }

        [XmlElement(ElementName = "dlstart")]
        public String StartTime
        {
            get { return startTime.ToString(); }
            set
            {
                startTime.setValue(value);
            }

        }

        [XmlElement(ElementName = "dlduration_hmsf")]
        public String DlDuration
        {
            get { return duration.ToString(); }
            set { duration.setValue(value); }
        }

        [XmlElement(ElementName = "dlspname")]
        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        [XmlElement(ElementName="dlhnumber")]
        public String Dlhnumber
        {
            get { return mediaID; }
            set { mediaID = value; }
        }

        [XmlAttribute(AttributeName = "sevtype")]
        public String Sevtype
        {
            get { return sevtype.ToString(); }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    return;
                }

                sevtype = Convert.ToInt32(value);

                if (spottype != -1)
                {
                    if (sevtype == 0 && spottype == 0)
                    {
                        m_eventType = EventType.Primary;
                    }
                    else
                    {
                        m_eventType = EventType.Bug;
                    }
                }
            }
        }

        [XmlElement(ElementName = "spottype")]
        public String Spottype
        {
            get { return spottype.ToString(); }
            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    return;
                }

                spottype = Convert.ToInt32(value);

                if (sevtype != -1)
                {
                    if (sevtype == 0 && spottype == 0)
                    {
                        m_eventType = EventType.Primary;
                    }
                    else
                    {
                        m_eventType = EventType.Bug;
                    }
                }
            }
        }

        #endregion Properties

    }
}
